package com.swinw.asumall.product.service.impl;

import com.swinw.asumall.product.dao.AttrAttrgroupRelationDao;
import com.swinw.asumall.product.dao.AttrDao;
import com.swinw.asumall.product.entity.AttrAttrgroupRelationEntity;
import com.swinw.asumall.product.entity.AttrEntity;
import com.swinw.asumall.product.service.AttrAttrgroupRelationService;
import com.swinw.asumall.product.vo.AttrGroupWithAttrsVo;
import com.swinw.asumall.product.vo.SkuItemVo;
import com.swinw.asumall.product.vo.SpuItemAttrGroupVo;
import com.swinw.common.utils.AttrTypeValue;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swinw.common.utils.PageUtils;
import com.swinw.common.utils.Query;

import com.swinw.asumall.product.dao.AttrGroupDao;
import com.swinw.asumall.product.entity.AttrGroupEntity;
import com.swinw.asumall.product.service.AttrGroupService;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {


    @Autowired
    private AttrAttrgroupRelationDao relationDao;

    @Resource
    private AttrDao attrDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catId) {
        String key = (String) params.get("key"); //获取检索条件，输入的key可能是属性分组Id，也有可能是属性分组名
        QueryWrapper<AttrGroupEntity> queryWrapper = new QueryWrapper<AttrGroupEntity>();//封装查询条件
        if(!StringUtils.isEmpty(key)){//如果我们传递的key不为空
            queryWrapper.and((elem)->{
                elem.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        if(catId != 0){ //如果没有传递三级分类ID，那么就查询全部属性分组信息
            queryWrapper.eq("catelog_id",catId);//封装查询条件
        }
        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 查询三级分类的属性
     *
     * 1、通过给定的三级分类，获得组ID（arrt_group_id）和组名(attr_group_name)
     * 2、通过组ID 从 属性&属性分组关联表中获得属性ID（attr_id）
     * 3、通过属性ID获取属性信息
     *
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVo> attrGroupWithattr(Long catelogId) {
        //1、通过三级分类查询分组信息
        QueryWrapper<AttrGroupEntity> groupQueryWrapper = new QueryWrapper<>();
        groupQueryWrapper.eq("catelog_id",catelogId);
        List<AttrGroupEntity> groupEntities = this.list(groupQueryWrapper);

        //2、通过组ID查询属性Id
        List<AttrGroupWithAttrsVo> collect = groupEntities.stream().map((attrGroup) -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(attrGroup, attrGroupWithAttrsVo);

            Long groupId = attrGroup.getAttrGroupId();
            QueryWrapper<AttrAttrgroupRelationEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("attr_group_id",groupId);
            List<AttrAttrgroupRelationEntity> relationEntities = relationDao.selectList(queryWrapper);

            //3、通过属性ID查询属性
            relationEntities.forEach((item)->{
                Long attrId = item.getAttrId();
                QueryWrapper<AttrEntity> wrapper = new QueryWrapper<>();
                wrapper.eq("attr_id",attrId);
                wrapper.eq("attr_type", AttrTypeValue.SPWCI_VALUE);
                List<AttrEntity> entities = attrDao.selectList(wrapper);
                attrGroupWithAttrsVo.setAttrs(entities);
            });
            return attrGroupWithAttrsVo;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 查出当前的Spu规格属性信息
     * 1、当前Spu所有的属性分组
     * 2、
     * @param spuId
     * @return
     */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        List<SpuItemAttrGroupVo> catelogId = this.baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return catelogId;
    }

}